*ALTERNATIVE DV Perceptions of discrimiation

*Print Figures corresponding to .do file title

clear all
use "ESS.dta"

*ETHNORACIAL

*Generate actual discrimination for ethnicity


g PD_raceA=0 if predetn==0  
replace PD_raceA=1 if predetn==3 | predetn==4  | predetn==2 | predetn==1  
replace PD_raceA=. if Ingroup==1
*g PD_raceA= predetn/4
 
*Generate merit variable for gender

replace pdaprp= pdaprpa if pdaprp==.
 
g PD_genderA=.
replace PD_genderA=0 if pdaprp==1 | pdaprp==2  | pdaprp==3    
replace PD_genderA=1 if pdaprp==4 | pdaprp==5  
replace PD_genderA=. if Female==0
 
 
 global y1 "PD_genderA"
 global y2 "PD_raceA" 
 global x1 "total_eduyrs"
 global z1 "Age  Edu_mum Female Edu_mum Africa Asia   Muslim i.Country  i.essround"
 global z2 "  Edu_mum i.Country Female Edu_mum Africa Asia   Muslim i.essround"
 global IV "T r"
 global z1b "Age Age2 Female Edu_mum Africa Asia   Muslim   i.Country  i.essround"
 global z2b "  Edu_mum   Africa Asia    Muslim  i.Country i.essround"



*********************************************************************************************************


*PERCEIVED DISCRIMINATION
 
la var total_eduyrs "Years of education"


quietly: reg $y2  $x1  
est store Correlation
local N1=  e(N)

quietly:  reg $y2 $x1 $z2b if  Monotonic==1, robust
est store OLS
local N2=  e(N)

drop total_eduyrs
gen total_eduyrs=eduyrs
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"

 
quietly:  rdbwselect  $y2  r if Monotonic==1,    covs(Edu_mum Female Africa Asia Muslim     r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9   c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24)
local u1 =floor(e(h_mserd))
g w=max(0,`u1'-abs(r)) 



quietly:  ivreg2 $y2  ($x1 = $IV) $z2b  [pweight=w]  if Monotonic==1, first baselevels robust gmm2s 
est store IV_restricted
mat l e(first)
local Fm= floor(e(first)[rownumb(e(first),"SWF"),1])
local N3=  e(N)
su  $y2 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Meanm=trim("`Mean'")
drop w

quietly:  rdbwselect  $y2  r if Strong==1,    covs(Edu_mum Female Africa Asia Muslim      r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9   c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24)
local u2 =floor(e(h_mserd))
g w=max(0,`u2'-abs(r)) 

quietly: ivreg2 $y2  ($x1 = $IV)  $z2b  [pweight=w]  if Strong==1, first baselevels robust   gmm2s  
est store IV_all
mat l e(first)
local Fs= floor(e(first)[rownumb(e(first),"SWF"),1])
local N4= e(N)
su  $y2 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Means=trim("`Mean'")
drop w


	   grstyle init
	 grstyle set legend 2,  nobox
	 grstyle set size 8pt: tick_label key_label
	 grstyle set size 12pt: heading
	 grstyle set size 10pt: subheading axis_title
	grstyle set color "180 180 180" "130 130 130" "65 65 65"  "0 0 0" 
    grstyle set graphsize 13cm 11.5cm 	
**# Bookmark #2
coefplot  (Correlation, label( "Correlation" "{it:N}=`N1'")) (OLS, label("OLS" "{it:N}=`N2'") )   (IV_all, label("IV (all)" "{it:N}=`N3'" )  )  (IV_restricted, label ("IV (strong)" "{it:N}=`N4'" )  ) ,   xscale(range(-0.2,0.2)) xlab(-0.2(.05)0.2, grid gstyle(minor))    keep( total_eduyrs)    xline(0,  lpattern(dash)  )   msymbol(d)  levels(90) ciopts(recast(. rcap))  xtitle("Victim of ethnic prejudice during last year", size(medsmall))    ylabel(,angle(vertical) labsize(medsmall))  legend(position(12) rows(1) span )  baselevels     title("{bf:Ethnoracial attitudes}")  note("OLS, IV(all) and IV(strong)  control for gender,ethnicity,maternal education,country and round effects." "Bandwidths in IV strategies are chosen using  MSE-optimal bandwidth selector." "IV(all) specification: 1st stage {it:F}-stat = `Fm' | bandwidth = `u1' | Outcome mean = `Meanm'." "IV(strong) specification:  1st stage {it:F}-stat = `Fs' | bandwidth = `u2' | Outcome mean= `Means'.", size(vsmall) span)
graph export "AppendixB_4a.png", replace 


drop total_eduyrs
gen total_eduyrs=  total_eduyrs2
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"
 
	
 
*GENDER

quietly: reg  $y1   $x1  
est store Correlation
local N1=  e(N)
 
quietly:reg  $y1   $x1  $z1  if Monotonic==1, robust
est store OLS
local N2=  e(N)
 
 drop total_eduyrs
gen total_eduyrs=eduyrs
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"
 
quietly:rdbwselect  $y1  r if Monotonic==1,  covs(Edu_mum Female r1 r2 r3 r4 r5 r6 r7 r8  c1 c2 c3 c4 c5 c6 c7 c8 c9   c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24)
local u1 =floor(e(h_mserd))
g  w=max(0,`u1'-abs(r)) 


quietly: ivreg2  $y1  ($x1 = $IV)    $z2 [pweight= w] if Monotonic==1, first  baselevels robust   gmm2s 
est store IV_all
mat l e(first)
local Fm= floor(e(first)[rownumb(e(first),"SWF"),1])
local N3=  e(N)
su  $y1 if e(sample), mean
loc Meanm:di%8.2fc r(mean)
drop w


quietly: rdbwselect  $y1  r if Strong==1,  covs(Edu_mum Female  r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9  c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24)
local u2 =floor(e(h_mserd))
g w=max(0,`u2'-abs(r)) 

quietly: ivreg2  $y1  ($x1 = $IV)    $z2  [pweight=w] if Strong==1, first  baselevels robust   gmm2s  
est store IV_restricted
mat l e(first)
local Fs= floor(e(first)[rownumb(e(first),"SWF"),1])
local N4= e(N)
su  $y1 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Means=trim("`Mean'")
drop w


	   grstyle init
	 grstyle set legend 2,  nobox
	 grstyle set size 8pt: tick_label key_label
	 grstyle set size 12pt: heading
	 grstyle set size 10pt: subheading axis_title
	grstyle set color   "180 180 180" "130 130 130" "65 65 65"  "0 0 0" 
    grstyle set graphsize 13cm 11.5cm 	
	coefplot  (Correlation, label( "Correlation" "{it:N}=`N1'") ) (OLS, label("OLS" "{it:N}=`N2'") )   (IV_all, label("IV (all)" "{it:N}=`N3'" ))  (IV_restricted, label ("IV (strong)" "{it:N}=`N4'" ) ) ,     keep( total_eduyrs)   xscale(range(-0.4,0.4)) xlab(-0.4(.1)0.4, grid gstyle(minor))   xline(0,  lpattern(dash)  )   msymbol(d)  levels(90) ciopts(recast(. rcap))  xtitle("Unfair pay given efforts and achievements", size(medsmall))    ylabel(,angle(vertical) labsize(medsmall))  legend(position(12) rows(1) span )  baselevels     title("{bf:Gender attitudes}")  note("OLS, IV(all) and IV(strong) control for gender,maternal education,country and round effects." "Bandwidths in IV strategies are chosen using  MSE-optimal bandwidth selector." "IV(all) specification: 1st stage {it:F}-stat = `Fm' | bandwidth = `u1' | Outcome mean = `Meanm'." "IV(strong) specification: 1st stage {it:F}-stat = `Fs' | bandwidth = `u2' | Outcome mean = `Means'.", size(vsmall) span)
graph export "AppendixB_4b.png", replace 
  
 